{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import MySQLdb\n",
    "db = MySQLdb.connect(host = 'localhost',#本地数据库\n",
    "                             user = 'root', #用户名\n",
    "                             passwd = 'tw123123', #数据库密码\n",
    "                             db = 'mydict', #数据库名\n",
    "                             charset = 'utf8') \n",
    "output = open('Synonym.md','w',encoding='utf8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_dic = open('data.json','r',encoding='utf8').read()\n",
    "my_dic = json.loads(my_dic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "def create_table(ch_def):\n",
    "    sql = \"\"\"call words_by_def('%s')\"\"\"%(ch_def)\n",
    "    cursor = db.cursor()\n",
    "    cursor.execute(sql)\n",
    "    word_list = cursor.fetchall()\n",
    "    word_list = [w[0] for w in word_list]\n",
    "\n",
    "    res = '|word|meaning|word|meaning|\\n|---|---|---|---|\\n'\n",
    "\n",
    "    for i in range(len(word_list)):\n",
    "        w = word_list[i]\n",
    "        print(w)\n",
    "        w_info = my_dic[w]\n",
    "        meaning = w_info['chinese'].strip().replace('\\n','<br/>')\n",
    "        if i%2 != 0:\n",
    "            res = res + w + '|' + meaning + '|\\n'\n",
    "        else:\n",
    "            res = res + '|' + w + '|' + meaning + '|'\n",
    "    if i % 2 == 0:\n",
    "        res = res + '| | |\\n'\n",
    "\n",
    "    res = '**' + ch_def + ':**\\n' + res + '\\n\\n'\n",
    "    return res\n",
    "\n",
    "\n",
    "def write_md(ch_def):\n",
    "    output = open('Synonym.md','w',encoding='utf8')\n",
    "    output.write('# Synonyms\\n\\n')\n",
    "    for d in ch_def:\n",
    "        res = create_table(d)\n",
    "        output.write(res)\n",
    "    output.close()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuse\n",
      "assail\n",
      "berate\n",
      "blameworthy\n",
      "castigate\n",
      "censorious\n",
      "censure\n",
      "chide\n",
      "condemnation\n",
      "denounce\n",
      "diatribe\n",
      "excoriate\n",
      "fulminate\n",
      "harangue\n",
      "hurl\n",
      "lacerate\n",
      "lambaste\n",
      "obloquy\n",
      "rail\n",
      "rant\n",
      "reactionary\n",
      "rebuke\n",
      "reprehensible\n",
      "reprimand\n",
      "reproof\n",
      "reprove\n",
      "revile\n",
      "scathing\n",
      "snub\n",
      "tirade\n",
      "unexceptionable\n",
      "unimpeachable\n",
      "upbraid\n",
      "vituperate\n",
      "badinage\n",
      "boo\n",
      "caricature\n",
      "deride\n",
      "flout\n",
      "gibe\n",
      "jest\n",
      "jibe\n",
      "mockery\n",
      "parody\n",
      "ridicule\n",
      "scoff\n",
      "scorn\n",
      "sneer\n",
      "taunt\n",
      "boorish\n",
      "churlish\n",
      "crass\n",
      "curt\n",
      "discourteous\n",
      "gauche\n",
      "impertinent\n",
      "impudent\n",
      "insolent\n",
      "laconic\n",
      "loutish\n",
      "uncouth\n",
      "affable\n",
      "amiability\n",
      "amiable\n",
      "benign\n",
      "bland\n",
      "bonhomie\n",
      "cordial\n",
      "genial\n",
      "ductile\n",
      "emollient\n",
      "lank\n",
      "limber\n",
      "limp\n",
      "lissome\n",
      "lithe\n",
      "lustrous\n",
      "pliable\n",
      "pliant\n",
      "spongy\n",
      "supple\n",
      "atrocious\n",
      "atrocity\n",
      "barbarous\n",
      "ferocity\n",
      "heinous\n",
      "malice\n",
      "malicious\n",
      "nefarious\n",
      "outrageous\n",
      "truculent\n",
      "tyrannical\n",
      "venom\n",
      "venomous\n",
      "vicious\n",
      "virulent\n",
      "vitriolic\n",
      "avarice\n",
      "avaricious\n",
      "avid\n",
      "covetous\n",
      "gluttonous\n",
      "gobble\n",
      "gorge\n",
      "mercenary\n",
      "miserly\n",
      "rapacious\n",
      "ravenous\n",
      "sordid\n",
      "voracious\n",
      "audacious\n",
      "bashfulness\n",
      "cursory\n",
      "curt\n",
      "daredevil\n",
      "effrontery\n",
      "gall\n",
      "hasty\n",
      "headlong\n",
      "impertinent\n",
      "impetuous\n",
      "imprudent\n",
      "impudent\n",
      "indiscretion\n",
      "perfunctory\n",
      "precipitate\n",
      "reckless\n",
      "slapdash\n",
      "slipshod\n",
      "sloppy\n",
      "temerity\n",
      "adamant\n",
      "bigot\n",
      "dogged\n",
      "headstrong\n",
      "implacable\n",
      "intransigent\n",
      "mulish\n",
      "obdurate\n",
      "obstinacy\n",
      "obstinate\n",
      "obstinateness\n",
      "opinionated\n",
      "pertinacious\n",
      "relentless\n",
      "rigid\n",
      "self-absorbed\n",
      "tenacious\n",
      "tenacity\n",
      "unyielding\n",
      "wrongheaded\n",
      "banal\n",
      "bathetic\n",
      "bromide\n",
      "cliche\n",
      "fusty\n",
      "hackney\n",
      "hackneyed\n",
      "hoary\n",
      "mediocre\n",
      "nondescript\n",
      "nonentity\n",
      "platitude\n",
      "platitudinous\n",
      "prosaic\n",
      "stodgy\n",
      "threadbare\n",
      "timeworn\n",
      "trite\n",
      "vulgar\n",
      "accede\n",
      "beseech\n",
      "conjure\n",
      "crave\n",
      "entreat\n",
      "importune\n",
      "invoke\n",
      "petition\n",
      "solicit\n",
      "suppliant\n",
      "supplicate\n",
      "blur\n",
      "cryptic\n",
      "diaphanous\n",
      "murky\n",
      "nebulous\n",
      "obfuscate\n",
      "obscure\n",
      "opacity\n",
      "opaque\n",
      "vagueness\n",
      "abstruse\n",
      "arcane\n",
      "convoluted\n",
      "cryptic\n",
      "elusive\n",
      "esoteric\n",
      "hermetic\n",
      "opacity\n",
      "profundity\n",
      "recondite\n",
      "turgid\n",
      "unintelligible\n",
      "forebode\n",
      "hunch\n",
      "premonition\n",
      "presage\n",
      "anachronism\n",
      "anachronistic\n",
      "antediluvian\n",
      "antiquated\n",
      "archaic\n",
      "bygone\n",
      "fusty\n",
      "moribund\n",
      "moth-eaten\n",
      "obsolescent\n",
      "outmoded\n",
      "shopworn\n",
      "timeworn\n",
      "bridle\n",
      "bumptious\n",
      "cavalier\n",
      "contemptuous\n",
      "effrontery\n",
      "haughty\n",
      "hubris\n",
      "imperious\n",
      "insolence\n",
      "insolent\n",
      "lofty\n",
      "overbearing\n",
      "overweening\n",
      "patronizing\n",
      "peremptory\n",
      "pomposity\n",
      "pompous\n",
      "pontificate\n",
      "presumptuous\n",
      "spurn\n",
      "supercilious\n",
      "upstage\n",
      "anarchy\n",
      "clutter\n",
      "commotion\n",
      "disarray\n",
      "discombobulate\n",
      "discompose\n",
      "disorganize\n",
      "disrupt\n",
      "embroil\n",
      "entangle\n",
      "faze\n",
      "fluster\n",
      "havoc\n",
      "jumble\n",
      "muddle\n",
      "muddy\n",
      "pandemonium\n",
      "rabble\n",
      "roil\n",
      "sloppy\n",
      "snarl\n",
      "tangle\n",
      "topsy-turvy\n",
      "tumult\n",
      "turbid\n",
      "turbulent\n",
      "turmoil\n",
      "welter\n",
      "bromide\n",
      "composed\n",
      "composure\n",
      "limpid\n",
      "lull\n",
      "lullaby\n",
      "mitigate\n",
      "phlegmatic\n",
      "repose\n",
      "sedate\n",
      "sedative\n",
      "serene\n",
      "unflappable\n",
      "aspersion\n",
      "besmirch\n",
      "calumniate\n",
      "calumny\n",
      "defamation\n",
      "denigrate\n",
      "deprave\n",
      "derogatory\n",
      "diatribe\n",
      "injurious\n",
      "invective\n",
      "libelous\n",
      "malign\n",
      "obloquy\n",
      "scandalous\n",
      "slanderous\n",
      "slur\n",
      "vilify\n"
     ]
    }
   ],
   "source": [
    "ch_def = ['责骂|斥责|训斥|指责','嘲笑','粗鲁','和蔼','柔软',\n",
    "          '凶残|恶毒|残暴','贪婪','鲁莽|草率','固执','陈腐|平庸|陈词滥调',\n",
    "         '乞求|恳求|请求','模糊','难懂|深奥','预感','过时|废弃的',\n",
    "          '傲慢','混乱','镇定|镇静','诽谤','新生的']\n",
    "write_md(ch_def)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
